//
//  GRRootTabBarController.m
//  YCGRICE
//
//  Created by Feng瘾 on 2018/7/3.
//  Copyright © 2018年 刘峰. All rights reserved.
//

#import "GRRootTabBarController.h"
#import "GRBargainListViewController.h"

#import "GRMyOwnViewController.h"
#import "GRRootNavigationController.h"
#import "GRScanViewController.h"

@interface GRRootTabBarController ()

@end

@implementation GRRootTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBar.tintColor       = HEXCOLOR(0x2D3037);
    self.tabBar.barTintColor    = [UIColor whiteColor];
    self.tabBar.backgroundColor = [UIColor whiteColor];
//    GRRootNavigationController *installment = [self newRootViewController:@"GRInstallmentViewController" TabbarTitle:@"分期购" TabbarImgName:@"amortizeNormal" TabbarSelectedImgName:@"amortizeSelected"];
    GRRootNavigationController *bargain = [self newRootViewController:@"GRBargainListViewController" TabbarTitle:@"砍价" TabbarImgName:@"bargaining_02" TabbarSelectedImgName:@"bargaining_03"];

//    GRRootNavigationController *home = [self newRootViewController:@"GRHoMeViewController" TabbarTitle:@"首页" TabbarImgName:@"amortizeNormal" TabbarSelectedImgName:@"amortizeSelected"];
    
    GRRootNavigationController *order = [self newRootViewController:@"GRScanViewController" TabbarTitle:@"扫一扫" TabbarImgName:@"scan_01" TabbarSelectedImgName:@"scan_02"];

    //    GRRootNavigationController *shopCar = [self newRootViewController:@"GRShopCarViewController" TabbarTitle:@"购物车" TabbarImgName:@"shopCarNormal" TabbarSelectedImgName:@"shopCarSelected"];
//    if (@available(iOS 10.0, *)) {
//        shopCar.tabBarItem.badgeColor = KGMTHEM_COLOR;
//        [shopCar.tabBarItem setBadgeTextAttributes:@{NSForegroundColorAttributeName : KGMBLACK_COLOR} forState:UIControlStateNormal];
//    }

//    GRRootNavigationController *person = [self newRootViewController:@"GRPersonViewController" TabbarTitle:@"我的" TabbarImgName:@"userNormal" TabbarSelectedImgName:@"userSelected"];
    GRRootNavigationController *person = [self newRootViewController:@"GRMyOwnViewController" TabbarTitle:@"我的" TabbarImgName:@"userNormal" TabbarSelectedImgName:@"userSelected"];
    
    self.viewControllers = @[bargain,order, person];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshBadgeValue:) name:GRUpdateShoppingCartCornerNotification object:nil];
}


- (GRRootNavigationController *)newRootViewController:(NSString *)vcName TabbarTitle:(NSString *)title TabbarImgName:(NSString *)imgName TabbarSelectedImgName:(NSString *)selectedImgName {
    UIViewController *viewController = [[NSClassFromString(vcName) alloc] init];
    GRRootNavigationController *navi = [[GRRootNavigationController alloc] initWithRootViewController:viewController];
    navi.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self imageOriginal:imgName] selectedImage:[self imageOriginal:selectedImgName]];
    return navi;
}

- (UIImage *)imageOriginal:(NSString *)imgName {
    UIImage *image = [[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return image;
}

+(void)seletedTabIndex:(NSInteger)index{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    GRRootTabBarController * tabarVC = (GRRootTabBarController*)window.rootViewController;
     tabarVC.selectedIndex = index;
}



/**
 刷新购物车角标

 @param notification <#notification description#>
 */
- (void)refreshBadgeValue:(NSNotification *)notification {
    NSString *badgeValue = notification.userInfo[@"badgeValue"];
    UINavigationController *shopcar   = self.viewControllers[1];
    if (badgeValue && [badgeValue integerValue] > 0) {
        shopcar.tabBarItem.badgeValue = badgeValue;
    }else {
        shopcar.tabBarItem.badgeValue = nil;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
